import {
    getUserInfo,
    setUserInfo,
    logout
} from '@/utils/auth'

import {
    api
} from '@/api/api'
const user = {
    state: {
        username: ''
    },
    mutations: {
        SET_ACCOUNT: (state, username) => {
            state.username = username;
        }
    },
    actions: {
        // 登录
        login({
            commit
        }, params) {

            api.manage.login(params).then((res) => {
                if (res.data && res.data.username) {
                    console.log(res)
                        // let data = res.data;

                    // commit('SET_ACCOUNT', params.account);

                    // setUserInfo({
                    //     account: params.account
                    // });
                    commit('SET_ACCOUNT', res.data.username);

                    setUserInfo({
                        username: res.data.username,
                        personId: res.data.personId
                    });
                }
                params.callback && params.callback(res);
            });
        },
        // 退出登录
        logout({
            commit
        }, params) {
            commit('SET_ACCOUNT', '');
            logout()
            let url = `${window.loginUrl}${location.origin}`;
            window.location.href = url;
        }
    }
};

export default user;